package com.servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guestbook.dao.GuestBookDAO;
import com.guestbook.dao.impl.GuestBookDAOJdbc;
import com.tools.Pagination;

public class GetMessageServlet extends HttpServlet {

	private static final long serialVersionUID = 7077913813820406709L;
	private int pageSize;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNumber;
		try {
			pageNumber = Integer.parseInt(request.getParameter("p"));
		} catch (Exception e) {
			pageNumber = 1;
		}
		GuestBookDAO dao=new GuestBookDAOJdbc();
		Pagination pager=dao.getGuestBookByPage(pageSize, pageNumber);
		request.setAttribute("guestbook.pager", pager);
		request.getRequestDispatcher("/getMessage.jsp").forward(request,
				response);

	}
	public void init(ServletConfig config) throws ServletException{
		super.init();
		try{
			this.pageSize=Integer.parseInt(config.getServletContext().getInitParameter("pageSize"));
		}catch(Exception e){
			this.pageSize=10;
		}
	}//在web.xml中添加<context-param>标记 获取pageSize
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
